local tugui = fk.CreateSkill{
    name = "wk_heg__tugui",
}

Fk:loadTranslationTable{
    ["wk_heg__tugui"] = "图归",
    [":wk_heg__tugui"] = "①每回合限一次，当你失去最后的手牌后或当你进入濒死状态后，你可获得与你距离为1的其他角色的一张手牌并展示之；②出牌阶段结束时，若你未失去以此法获得的所有牌，你移除此武将牌。",

    ["#wk_heg__tugui-ask"] = "图归：是否获得与你距离为1的其他角色的一张手牌",
    ["#wk_heg__tugui-choose"] = "图归：选择一名与你距离为1的其他角色",

    ["$wk_heg__tugui1"] = "矢志于北，尽忠于国。",
    ["$wk_heg__tugui2"] = "命系袁氏，一心向北。",
}

local H = require "packages/ol_hegemony/util"

tugui:addEffect(fk.AfterCardsMove,{
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    local ret = false
    if not player:hasSkill(tugui.name) or not player:isKongcheng() then return end
    for _, move in ipairs(data) do
      if move.from == player then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand and player:usedSkillTimes(tugui.name, Player.HistoryTurn) == 0 then
            ret = true
            break
          end
        end
      end
    end
    if ret then
      return table.find(player.room.alive_players, function(p) return player:distanceTo(p) == 1 and not p:isKongcheng() end)
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = tugui.name, prompt = "#wk_heg__tugui-ask"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p) return player:distanceTo(p) == 1 and not p:isKongcheng() end)
    if #targets > 0 then
      local to = room:askToChoosePlayers(player,{
          targets = targets,
          min_num = 1,
          max_num = 1,
          prompt = "#wk_heg__tugui-choose",
          skill_name = tugui.name,
          cancelable = false,
      })
      local card = room:askToChooseCard(player,{
          target = to[1],
          flag = "h",
          skill_name = tugui.name,
      })
      room:obtainCard(player, card, false, fk.ReasonPrey)
      player:showCards(card)
      local mark = player:getTableMark("wk_heg__tugui")
      table.insertIfNeed(mark, card)
      room:setPlayerMark(player, "wk_heg__tugui", mark)
    end
  end,
})

tugui:addEffect(fk.EnterDying,{
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(tugui.name) and player:usedSkillTimes(tugui.name, Player.HistoryTurn) == 0
      and table.find(player.room.alive_players, function(p) return player:distanceTo(p) == 1 and not p:isKongcheng() end)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = tugui.name, prompt = "#wk_heg__tugui-ask"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function(p) return player:distanceTo(p) == 1 and not p:isKongcheng() end)
    if #targets > 0 then
      local to = room:askToChoosePlayers(player,{
          targets = targets,
          min_num = 1,
          max_num = 1,
          prompt = "#wk_heg__tugui-choose",
          skill_name = tugui.name,
          cancelable = false,
      })
      local card = room:askToChooseCard(player,{
          target = to[1],
          flag = "h",
          skill_name = tugui.name,
      })
      room:obtainCard(player, card, false, fk.ReasonPrey)
      player:showCards(card)
      local mark = player:getTableMark("wk_heg__tugui")
      table.insertIfNeed(mark, card)
      room:setPlayerMark(player, "wk_heg__tugui", mark)
    end
  end,
})

tugui:addEffect(fk.EventPhaseEnd,{
  anim_type = "defensive",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(tugui.name) and target == player and player.phase == Player.Play and player:getMark("wk_heg__tugui") ~= 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, t in ipairs(player:getMark("wk_heg__tugui")) do
      if table.contains(player:getCardIds("he"), t) then
        local isDeputy = H.inGeneralSkills(player, tugui.name)
        if isDeputy then
          H.removeGeneral(player, isDeputy == "d")
        end
        break
      end
    end
    room:setPlayerMark(player, "wk_heg__tugui", 0)
  end,
})

return tugui